/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import xmlobject.DTO.OrderItems.OrderItem;
import xmlobject.DTO.OrderItems.OrderItems;

/**
 *
 * @author minhsong
 */
public class OrderItemDAL extends Dataconection {

    private OrderItem ReadOrderItemFromDS(ResultSet rs){
        OrderItem result = new OrderItem();
        if(rs!=null){
            try {
                result.setItemID(rs.getLong("ItemID"));
                result.setOrderID(rs.getLong("OrderID"));
                result.setProductID(rs.getLong("ProductID"));
                result.setQuantity(rs.getLong("Quantity"));
                result.setPrice(rs.getDouble("Price"));
                result.setCreateBy(rs.getString("CreateBy"));
                result.setCreateDate(rs.getDate("CreateDate"));
            } catch (SQLException ex) {
                Logger.getLogger(ProductDAL.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }
    
    public OrderItem getOrderItemById(int id) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call sporder_itemGet(?) }");
             proc_stmt.setLong("ItemID", id);
            rs = proc_stmt.executeQuery();
            if(rs.next())
            {
                return  ReadOrderItemFromDS(rs);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public OrderItems getAllOrderItemByOrderId(int id) {
        OrderItems result = new OrderItems();
        try {
            proc_stmt = DAOConnection.prepareCall("{ call sporder_itemSelectAllByOrderID(?) }");
            proc_stmt.setLong("OrderID", id);
            rs = proc_stmt.executeQuery();
            while(rs.next())
            {
                result.getOrderItem().add(ReadOrderItemFromDS(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public OrderItem Create(OrderItem value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call sporder_itemInsert(?,?,?,?,?,?) }");
            proc_stmt.registerOutParameter("ItemID", java.sql.Types.INTEGER);
            proc_stmt.setLong("OrderID",value.getOrderID());
            proc_stmt.setLong("ProductID",value.getProductID());
            proc_stmt.setLong("Quantity",value.getQuantity());
            proc_stmt.setDouble("Price",value.getPrice());
            proc_stmt.setString("CreateBy",value.getCreateBy());
            proc_stmt.execute();
            value.setItemID(proc_stmt.getLong("ItemID"));
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return value;
    }

    public OrderItem Edit(OrderItem value) {
         try {
            proc_stmt = DAOConnection.prepareCall("{ call sporder_itemUpdate(?,?,?,?,?) }");
            proc_stmt.setLong("ItemID", value.getItemID());
            proc_stmt.setLong("OrderID",value.getOrderID());
            proc_stmt.setLong("ProductID",value.getProductID());
            proc_stmt.setLong("Quantity",value.getQuantity());
            proc_stmt.setDouble("Price",value.getPrice());
            proc_stmt.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return value;
    }

    public boolean Delete(int  value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call sporder_itemDelete(?) }");
            proc_stmt.setLong("ItemID", value);
            return  proc_stmt.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public boolean DeleteAllOrderItemsByOrderId(int  value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call sporder_itemDeleteAllByOrderID(?) }");
            proc_stmt.setLong("OrderID", value);
            return  proc_stmt.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

}
